package org.vineatoum.view.action;

import javax.swing.JPanel;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.RowSpec;
import com.jgoodies.forms.factories.FormFactory;

import javax.swing.DefaultListModel;
import javax.swing.DropMode;
import javax.swing.ImageIcon;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JList;
import javax.swing.JLabel;

import org.vineatoum.business.Plantation;
import org.vineatoum.business.state.Recolte;
import org.vineatoum.business.state.abstraction.VinAbstract;
import org.vineatoum.model.abstraction.IVineaToumModel;
import org.vineatoum.view.utils.BusinessPanelFactory;
import org.vineatoum.view.utils.JPanelWrapped;
import org.vineatoum.view.utils.TransferableVinAbstract;

import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.UnsupportedFlavorException;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DragGestureEvent;
import java.awt.dnd.DragGestureListener;
import java.awt.dnd.DragGestureRecognizer;
import java.awt.dnd.DragSource;
import java.awt.dnd.DragSourceDragEvent;
import java.awt.dnd.DragSourceDropEvent;
import java.awt.dnd.DragSourceEvent;
import java.awt.dnd.DragSourceListener;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetDragEvent;
import java.awt.dnd.DropTargetDropEvent;
import java.awt.dnd.DropTargetEvent;
import java.awt.dnd.DropTargetListener;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import java.util.TooManyListenersException;

public class RecolteActionPanel extends JPanel implements DragSourceListener,
											DragGestureListener, DropTargetListener {
	private static final long serialVersionUID = -1708006372273549965L;
	
	private DefaultListModel RecolteListModel = new DefaultListModel();
	private IVineaToumModel model;
	private List<Plantation> plantationList;
	private DragSource dragSource = new DragSource();
	private DropTarget dropTarget = new DropTarget();
	
	private JList listBenne;
	private JList listPlantation ;
	private JList listRecolte ;
	
	
	private void initialization()
	{
		
	}
	/**
	 * Create the panel.
	 */
	public RecolteActionPanel() {
		
		
		setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
			new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,}));
		
		JLabel lblPlantation = new JLabel("Plantation");
		add(lblPlantation, "2, 2");
		
		JLabel lblRecolte = new JLabel("Recolte");
		add(lblRecolte, "4, 2");
		
		JLabel lblBenne = new JLabel("Benne");
		add(lblBenne, "6, 2");
		
		JScrollPane scrollPane = new JScrollPane();
		add(scrollPane, "2, 4, fill, fill");
		
		listPlantation = new JList();
		plantationList = Plantation.plantations;
		DefaultListModel dlm = new DefaultListModel();
		Iterator<Plantation> it = plantationList.iterator();
		while(it.hasNext())
		{
			Plantation obj = it.next();
			if(obj !=null)
			{
				
					dlm.addElement(obj)	;
			}
			
		}
	
		listPlantation.setModel(dlm);
		DragGestureRecognizer dgr = dragSource.createDefaultDragGestureRecognizer(listPlantation,DnDConstants.ACTION_COPY, this);
		
		listPlantation.setDropMode(DropMode.ON_OR_INSERT);
		scrollPane.setViewportView(listPlantation);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		add(scrollPane_1, "4, 4, fill, fill");
		
		listRecolte = new JList();
		listRecolte.setDropTarget(this.dropTarget);
		try {
			dropTarget.addDropTargetListener(new DropTargetListener(){

				@Override
				public void dragEnter(DropTargetDragEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void dragExit(DropTargetEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void dragOver(DropTargetDragEvent arg0) {
					// TODO Auto-generated method stub
					
				}

				@Override
				public void drop(DropTargetDropEvent arg0) {
					try {
						Plantation p = (Plantation) arg0.getTransferable().getTransferData(new DataFlavor( Plantation.class,DataFlavor.javaJVMLocalObjectMimeType));
						System.out.println(p);
						
						
						createNewRecolte(p);
						
					} catch (UnsupportedFlavorException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					
				}

				@Override
				public void dropActionChanged(DropTargetDragEvent arg0) {
					// TODO Auto-generated method stub
					
				}});
		} catch (TooManyListenersException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		listRecolte.setModel(RecolteListModel);
		scrollPane_1.setViewportView(listRecolte);
		
		JScrollPane scrollPane_2 = new JScrollPane();
		add(scrollPane_2, "6, 4, fill, fill");
		
		listBenne = new JList();
		scrollPane_2.setViewportView(listBenne);
		
		JLabel label = new JLabel(new ImageIcon("C:\\Users\\Totetmatt\\workspace\\vineatoum\\img\\trash.png"));
		add(label, "2, 6");
		initialization();
	}

	@Override
	public void dragEnter(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void dragExit(DropTargetEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void dragOver(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void drop(DropTargetDropEvent arg0) {
	
		
	}
	@Override
	public void dropActionChanged(DropTargetDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void dragGestureRecognized(DragGestureEvent arg0) {
		// TODO Auto-generated method stub
		JList list = (JList) arg0.getComponent();
		VinAbstract v = (VinAbstract) list.getSelectedValue();
		TransferableVinAbstract c = new TransferableVinAbstract(v);
	
		dragSource.startDrag(arg0, DragSource.DefaultCopyDrop, c, this);
		
	}
	@Override
	public void dragDropEnd(DragSourceDropEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void dragEnter(DragSourceDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void dragExit(DragSourceEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void dragOver(DragSourceDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void dropActionChanged(DragSourceDragEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	public void createNewRecolte(Plantation p)
	{
		
		Recolte r = new Recolte();
		JPanelWrapped pane = BusinessPanelFactory.createBusinessPanel(model, r);
		JDialog d = new JDialog();
		d.setModal(true);
		d.setTitle("Nouvelle Récolte");
		d.setSize(300, 300);
		JOptionPane jop = new JOptionPane(pane,
			    JOptionPane.QUESTION_MESSAGE,
			    JOptionPane.OK_CANCEL_OPTION);
		
		if(JOptionPane.showConfirmDialog(this,pane,"Nouvelle Récolte",JOptionPane.OK_CANCEL_OPTION)==JOptionPane.OK_OPTION)
		{
			RecolteListModel.addElement(r);
		}
		
		
		
	}

}
